package com.atguigu.rabbitmq.one;

import com.atguigu.rabbitmq.util.RabbitmqUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;

/**
 * @author codingBee
 * @since 2021/6/22/20:00
 */
public class Producer {
    //队列名称
    public static final String QUEUE_NAME = "hello";
    //发消息
    public static void main(String[] args) throws IOException{
        Connection connection = RabbitmqUtil.getConnection();
        //获取信道
        Channel channel = connection.createChannel();
        /**
         * Declare a queue 声明一个队列
         * @see com.rabbitmq.client.AMQP.Queue.Declare
         * @see com.rabbitmq.client.AMQP.Queue.DeclareOk
         * @param queue the name of the queue 队列名称
         * @param durable true if we are declaring a durable queue (the queue will survive a server restart) 队列里面的消息是否持久化（磁盘），默认情况消息是存储在内存中
         * @param exclusive true if we are declaring an exclusive queue (restricted to this connection) 该队列是否只供一个消费者进行消费
         * @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use) 是否自动删除，当最后一个消费者断开连接以后该队列是否删除
         * @param arguments other properties (construction arguments) for the queue 其他参数
         * @return a declaration-confirm method to indicate the queue was successfully declared
         * @throws java.io.IOException if an error is encountered
         */
        AMQP.Queue.DeclareOk declareOk = channel.queueDeclare(QUEUE_NAME, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, null);
        //消息体
        String  message = "helloWorld";
        /**
         * Publish a message. 发送一个消息
         *
         * Publishing to a non-existent exchange will result in a channel-level
         * protocol exception, which closes the channel.
         *
         * Invocations of <code>Channel#basicPublish</code> will eventually block if a
         * <a href="https://www.rabbitmq.com/alarms.html">resource-driven alarm</a> is in effect.
         *
         * @see com.rabbitmq.client.AMQP.Basic.Publish
         * @see <a href="https://www.rabbitmq.com/alarms.html">Resource-driven alarms</a>
         * @param exchange the exchange to publish the message to  要发送到的交换器
         * @param routingKey the routing key 路由 key
         * @param props other properties for the message - routing headers etc 其他信息的参数，如路由头
         * @param body the message body 消息体
         * @throws java.io.IOException if an error is encountered
         */
        channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
        System.out.println("消息发送完毕");
    }
}
